# coding=utf-8
import json
import sys
import time
from pactverify.matchers import Matcher, Like, EachLike, Term, Enum, PactVerify
import allure
import jsonpath
import pytest

from common.utils import Utils
from common.assert_operator import *
from common.db import *
from common.read_yaml import *

string_assert = string_assert() # 字符串校验类
pact = Pact() # 契约校验类
execute_sql = sql() # sql集合
utils = Utils()
data = ReadYaml_By_Root_Dict("case/02_approve_submit_test/02_2_FinishNode_Check.yml").get_yaml_data()  # 读取数据

@allure.feature('审批提交，异常校验')  # 测试报告显示测试功能
class Test_02_2_FinishNode_Check():

    # 获取yml文件中[审批确认路径校验]的数据参数
    approve_process_params = data['审批确认路径校验']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(approve_process_params,"$[*][0]")
    @pytest.mark.parametrize("title, param, expect", approve_process_params,ids=ids_list)
    def test_FinishNode_Check(self, user_login, title, param, expect):
        Logger.info("*************** {}开始执行用例 ***************".format(title))
        allure.dynamic.description(f"{param['description']}")  # # 动态解析用例描述在测试报告中展示
        ConfirmPath,ApproveResp =  "确认路径校验", "审批响应校验"
        user,process_name = param['user'],param['process_name']
        main = user_login.goto_Approve_Process(user)    # 登录到审批类实例
        process_instance_resp = main.open_process_instance(process_name)   # 通过流程名称打开流程实例
        valid_pass_resp = main.valid_pass(param,process_instance_resp)
        # 审批前校验请求返回体的code=2，表示提交审批校验不通过
        string_assert.eq(valid_pass_resp.json().get('code'), 2)
        confirm_resp = main.approve_confirm_path(param, process_instance_resp)  # 提交审批返回确认路径
        confirm_path_result = utils.pack_confirm_steps_field(confirm_resp.json(),['.tag'], ConfirmPath)
        dict_assert(expect[ConfirmPath],confirm_path_result)   # 确认路径校验
        response = main.approve_submit(param, process_instance_resp)    # 返回确认路径并提交
        string_assert.eq(response.status_code, 200)
        validateResult = jsonpath.jsonpath(response.json(),'$.data.validateResult')[0]
        print(f'审批响应校验:\n{validateResult}')
        dict_assert(expect[ApproveResp],validateResult)    # 审批响应校验

        Logger.info("*************** {}结束执行用例 ***************".format(title))
